A1 = [ones(m,1),X];

Z2 = A1*Theta1.';

A2 = sigmoid(Z2);

A2 = [ones(m,1),A2];

Z3=(A2*Theta2.');

A3 = sigmoid(Z3);

Y = zeros(size(A3));

y_mat=repmat(y,1,size(Y,2));

idx_mat = repmat([1:size(Y,2)],m,1);

Y=double(y_mat==idx_mat);
%Y=Y.';
J=mean(sum(-Y.*log(A3)-(ones(size(Y))-Y).*log(ones(size(Y))-A3),2))+...
(lambda/(2.*m)).*(sum(sum(Theta1(:,2:end).^2,2))+sum(sum(Theta2(:,2:end).^2,2)));

Delta3 = A3-Y;
Delta2 = (Delta3*Theta2(:,2:end)).*sigmoidGradient(Z2);

Delta2_acc = Delta3.'*A2(:,2:end)./m;
Delta1_acc = Delta2.'*A1(:,2:end)./m;
